package com.mondo.financialwallet.presentation.ui.fragment;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.common.ui.BaseFragment;
import com.mondo.financialwallet.R;
import com.mondo.financialwallet.data.DataRepository;
import com.mondo.financialwallet.data.entity.UserAssetsEntity;
import com.mondo.financialwallet.data.entity.UserInfoEntity;
import com.mondo.financialwallet.presentation.event.AlterNicknameEvent;
import com.mondo.financialwallet.presentation.event.ModifyAvatarEvent;
import com.mondo.financialwallet.presentation.event.RequestUserAssetEvent;
import com.mondo.financialwallet.presentation.ui.activity.AboutUsActivity;
import com.mondo.financialwallet.presentation.ui.activity.AssetOverviewActivity;
import com.mondo.financialwallet.presentation.ui.activity.BillingDetailsActivity;
import com.mondo.financialwallet.presentation.ui.activity.FeedbackActivity;
import com.mondo.financialwallet.presentation.ui.activity.PersonalSettingsActivity;
import com.mondo.financialwallet.presentation.ui.activity.WebViewActivity;
import com.mondo.financialwallet.presentation.ui.dialog.TipsDialog;
import com.mondo.financialwallet.presentation.utils.ActivityTitleBarHelper;

import org.greenrobot.eventbus.Subscribe;


import butterknife.Bind;
import jp.wasabeef.glide.transformations.BlurTransformation;

public class MyFragment extends BaseFragment {

    @Bind(R.id.avatar_image) ImageView mAvatarImage;
    @Bind(R.id.nickname_text_view) TextView mNicknameTextView;
    @Bind(R.id.billing_details_layout) View mBillingDetailsLayout;
    @Bind(R.id.total_assets_text_view) TextView mTotalAssetsTextView;
    @Bind(R.id.header_background_image) ImageView mHeaderBackgroundImage;
    @Bind(R.id.available_balance_text) TextView mAvailableBalanceTextView;
    @Bind(R.id.yesterday_earnings_text) TextView mYesterdayEarningsTextView;
    @Bind(R.id.feedback_layout) View mFeedbackLayout;
    @Bind(R.id.personal_settings_layout) View mPersonalSettingsLayout;
    @Bind(R.id.assets_overview_layout) View mAssetsOverviewLayout;
    @Bind(R.id.about_us_layout) View mAboutUsLayout;
//    @Bind(R.id.wallet_manage_layout) View mWalletManageLayout;
    @Bind(R.id.invite_friends_layout) View mInviteFriendsLayout;
    @Bind(R.id.my_dividend_layout) View mMyDividendLayout;
    @Bind(R.id.contact_us_layout) View mContactUsLayout;


    public static Fragment newInstance() {
        return new MyFragment();
    }

    @Override
    protected void setStatusBar(Activity activity) {
        ActivityTitleBarHelper.setTransparentStatus(activity);
    }

    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initActions() {
        // 用户资产
        requestUserAssets();

        // 用户信息
        requestUserInfoData();

        // 账单明细
        mBillingDetailsLayout.setOnClickListener(v ->
                BillingDetailsActivity.launch(v.getContext()));

        // 资产预览
        mAssetsOverviewLayout.setOnClickListener(v ->
                AssetOverviewActivity.launch(v.getContext()));

        // 钱包管理
//        mWalletManageLayout.setOnClickListener(v -> showNotOpen());

        // 关于我们
        mAboutUsLayout.setOnClickListener(v -> AboutUsActivity.launch(v.getContext()));

        // 意见反馈
        mFeedbackLayout.setOnClickListener(v -> FeedbackActivity.launch(v.getContext()));

        // 我的邀请
        mInviteFriendsLayout.setOnClickListener(v ->
                WebViewActivity.inviteFriends(v.getContext()));

        // 我的分红
        mMyDividendLayout.setOnClickListener(v ->
                WebViewActivity.myDividend(v.getContext()));
    }

    private void showNotOpen() {
        TipsDialog.warning(getContext())
                .build(R.string.text_tips, R.string.message_not_open)
                .show();
    }

    private void requestUserAssets() {
        DataRepository.get().userAssets(result -> {
            if (result.isSuccess()) {
                UserAssetsEntity.DataBean userAsset = result.getData();
                mTotalAssetsTextView.setText(userAsset.getTotal_asset()); // 总资产
                mAvailableBalanceTextView.setText(userAsset.getAvailable());   // 可用余额
                mYesterdayEarningsTextView.setText("+" + userAsset.getYesterday_profit()); // 昨日收益
            }
        });
    }

    private void requestUserInfoData() {
        DataRepository.get().userInfo(result -> {
            if (result.isSuccess()) {
                UserInfoEntity.DataBean userInfo = result.getData();
                displayUserInfoToView(userInfo);
            }
        });
    }

    private void displayUserInfoToView(UserInfoEntity.DataBean userInfo) {
        String uid = userInfo.getUid();
        String avatarImage = userInfo.getHead_image();
        String nickname = userInfo.getNickname();

        if (userInfo.getHead_image() != null) {
            // 头像
            Glide.with(mAvatarImage)
                    .load(avatarImage)
                    .dontAnimate()
                    .into(mAvatarImage);

            // 头像背景
            Glide.with(mHeaderBackgroundImage)
                    .load(avatarImage)
                    .dontAnimate()
                    .apply(RequestOptions.bitmapTransform(new BlurTransformation(25)))
                    .into(mHeaderBackgroundImage);
        }

        mNicknameTextView.setText(userInfo.getNickname()); // 昵称

        // 个人设置
        mPersonalSettingsLayout.setOnClickListener(v ->
                PersonalSettingsActivity.launch(v.getContext(), nickname, uid, avatarImage));

        // 个人设置
        mAvatarImage.setOnClickListener(v ->
                PersonalSettingsActivity.launch(v.getContext(), nickname, uid, avatarImage));

//        // 客服设置
//        Map<String, String> info = new HashMap<>();
//        String sdkToken = LocalConfigStorage.get().getUid();
//        info.put(UdeskConst.UdeskUserInfo.USER_SDK_TOKEN, sdkToken);
//        info.put(UdeskConst.UdeskUserInfo.NICK_NAME, userInfo.getNickname());
//        info.put(UdeskConst.UdeskUserInfo.EMAIL, userInfo.getEmail());
//        info.put(UdeskConst.UdeskUserInfo.CELLPHONE, userInfo.getMobile());
//        UdeskConfig.Builder builder = new UdeskConfig.Builder();
//        builder.setDefualtUserInfo(info);
//
//        // 启动客服
//        mContactUsLayout.setOnClickListener(v -> UdeskSDKManager.getInstance().entryChat(
//                getContext(), builder.build(), sdkToken));
    }

    @Subscribe
    public void alterNicknameEvent(AlterNicknameEvent event){
        mNicknameTextView.setText(event.getNickname());
    }

    @Subscribe
    public void requestUserAssetEvent(RequestUserAssetEvent event){
        requestUserAssets();
    }

    @Subscribe
    public void modifyAvatarEvent(ModifyAvatarEvent event){
        mHeaderBackgroundImage.setImageURI(event.getAvatar());
        mAvatarImage.setImageURI(event.getAvatar());
    }
}
